package com.jdd.dao.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdd.common.api.vo.Result;
import com.jdd.common.system.vo.LoginUser;
import com.jdd.common.util.LoginUserUtils;
import com.jdd.common.util.ResultGenerator;
import com.jdd.dao.entity.SysDepartArea;
import com.jdd.dao.entity.TProject;
import com.jdd.dao.service.ISysDepartAreaService;
import com.jdd.dao.service.TProjectService;
import com.jdd.dao.vo.SysAreaTreeModel;
import com.jdd.modules.system.entity.SysUserDepart;
import com.jdd.modules.system.service.ISysUserDepartService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ls
 * @since 2022-09-23
 */
@Slf4j
@RestController
@RequestMapping("/sysArea")
public class SysAreaController {
    @Resource
    private ISysDepartAreaService sysAreaService;

    @Resource
    private TProjectService tProjectService;

    @Resource
    private ISysUserDepartService sysUserDepartService;

    @PostMapping("/add")
    public Result add(@RequestBody SysDepartArea sysArea) {
        SysDepartArea parentArea = sysAreaService.getById(sysArea.getParentId());
        /*查找父级区域下的所有用户，并给用户授权*/
        sysArea.setParentName(parentArea.getName());
        sysArea.setSysProjectCode(parentArea.getSysProjectCode());
        sysAreaService.save(sysArea);
        List<String> userIds = new ArrayList<>();
        getUser(sysArea.getParentId(),userIds);
        for (String userId : userIds) {
            SysUserDepart sysUserDepart = new SysUserDepart();
            sysUserDepart.setDepId(sysArea.getId());
            sysUserDepart.setUserId(userId);
            sysUserDepart.setType(1);
            sysUserDepartService.save(sysUserDepart);
        }
        log.info("-------添加区域并给区域下用户绑定权限--------");
        return ResultGenerator.genSuccessResult("添加成功");
    }
    /*查找父级目录下的人员并封装*/
    public void getUser(String parentId,List<String> userIds){
        QueryWrapper<SysDepartArea> sysDepartAreaQueryWrapper = new QueryWrapper<>();
        sysDepartAreaQueryWrapper.eq("id",parentId);
        SysDepartArea sysAreaServiceOne = sysAreaService.getOne(sysDepartAreaQueryWrapper);
        QueryWrapper<SysUserDepart> sysUserDepartQueryWrapper = new QueryWrapper<>();
        sysUserDepartQueryWrapper.eq("dep_id",parentId);
        List<SysUserDepart> list = sysUserDepartService.list(sysUserDepartQueryWrapper);
        for (SysUserDepart sysUserDepart : list) {
            if (!userIds.contains(sysUserDepart.getUserId())){
                userIds.add(sysUserDepart.getUserId());
            }
        }
        if (sysAreaServiceOne.getParentId()!=null){
            getUser(sysAreaServiceOne.getParentId(),userIds);
        }
    }
    @GetMapping("/querySysAreaTreeList")
    public Result<List<SysAreaTreeModel>> querySysAreaTreeList() {
        Result<List<SysAreaTreeModel>> result = new Result<>();
        List<SysAreaTreeModel> list = sysAreaService.queryTreeList();
        result.setResult(list);
        result.setSuccess(true);
        return result;
    }

    @ApiOperation(value = "解绑", notes = "解绑")
    @DeleteMapping(value = "/removeBind")
    public Result<?> removeBind(@RequestParam(name = "id", required = true) String id) {
        try {
            /*先判断当前sysArea下是否存在用户或者其他区域*/

            sysAreaService.removeBind(id);
        } catch (Exception e) {
            return ResultGenerator.genFailResult("删除失败");
        }
        return ResultGenerator.genSuccessResult("删除成功");
    }

    /*删除区域*/
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam(name = "id",required = true)String id){
        String orgCode = LoginUserUtils.getLoginUser().getOrgCode();
        if( sysAreaService.count(new QueryWrapper<SysDepartArea>().eq("sys_project_code",orgCode))<=0){
            return ResultGenerator.genFailResult("必须存在一个区域");
        }
        if (sysAreaService.count(new QueryWrapper<SysDepartArea>().eq("parent_id",id))>0){
            return ResultGenerator.genFailResult("此区域下存在子项目或者子区域");
        }
        if (tProjectService.count(new QueryWrapper<TProject>().eq("sys_area_id",id))>0){
            return ResultGenerator.genFailResult("此区域下存在子项目或者子区域");
        }
        sysAreaService.removeById(id);
        return ResultGenerator.genSuccessResult("删除成功！！！");
    }
}
